﻿using ManaSky.Common.Model;
using ManaSky.DBCore.Attributes;
using ManaSky.Entity;
using ManaSky.IRepository;
using ManaSky.IService;
using ManaSky.Repository;
using ManaSky.SSO.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManaSky.Service
{
    /// <summary>
    /// 用户信息服务
    /// </summary>
    public class SysUsersService : FoundService<sys_user>,ISysUsersService
    {
        #region 构造
        public readonly ISysUsersRepository _sysUsersRepository;
        public SysUsersService(IAuth _auth, ISysUsersRepository sysUsersRepository) : base(_auth)
        {
            _sysUsersRepository = sysUsersRepository;
        }
        #endregion

        /// <summary>
        /// 使用用户名称查询用户信息
        /// </summary>
        /// <returns></returns>
        public async Task<OperateResult> GetUserByName(string name)
        {
            var select = _sysUsersRepository.Select.Where(t => t.LoginName.Trim() == name);
            select = select.Where(t => t.IsDel == 0);
            var info = await select.FirstAsync();
            return OperateResult.SuccessOperate(info);
        }
        public Task Delete(sys_user sys_Users)
        {
            throw new NotImplementedException();
        }

        public async Task<OperateResult> GetList()
        {
            var list = await _sysUsersRepository.GetListAsync();
            return OperateResult.SuccessOperate(list);
        }
        [Transaction]
        public async Task Save(sys_user sys_Users)
        {
            var ss =  await _sysUsersRepository.InsertOrUpdateAsync(sys_Users);
            throw new Exception("1111");
        }

        public Task Update(sys_user sys_Users)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="sys_Users"></param>
        /// <returns></returns>
        [Transaction]
        public async Task<OperateResult> UserLogin(sys_user sys_Users)
        {
            var bo = await _sysUsersRepository.UpdUserLasLoginTime(sys_Users.Id);
            if (!bo)
                return OperateResult.FailedOperate("更新失败");
            return OperateResult.SuccessOperate("");
        }
    }
}
